package com.qys.livesMall.app.user.controller.app;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.app.user.model.dto.AppBaseDTO;
import com.qys.livesMall.app.user.model.entity.AppUserConcern;
import com.qys.livesMall.app.user.model.vo.app.AppUserConcernVO;
import com.qys.livesMall.app.user.service.AppUserConcernService;
import com.qys.livesMall.app.user.service.IH2AppUserService;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.framework.utils.SecurityUtils;
import com.qys.livesMall.framework.view.BaseQuery;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户关注列表 前端控制器
 * </p>
 *
 * @author
 * @since 2023-06-02
 */
@RestController
@RequestMapping("/app/user/concern")
@Tag(name = "用户关注列表", description = "用户关注列表")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AppUserConcernController {

    private final AppUserConcernService userConcernService;
    private final IH2AppUserService appUserService;

    @PostMapping("/{concernId}")
    @Operation(
            summary = "用户关注/取消关注",
            parameters = {
                    @Parameter(name = "concernId", description = "关注用户id")
            }
    )
    public R<Void> concernByUserId(@PathVariable Long concernId) {
        userConcernService.concernByUserId(concernId, SecurityUtils.getUserId());
        return R.ok();
    }

    @PostMapping("/page/concerns")
    @Operation(summary = "查询用户关注列表")
    public R<Page<AppUserConcernVO>> pageUserConcern(@RequestBody BaseQuery baseQuery) {
        if (StringUtils.isBlank(baseQuery.getId())) {
            baseQuery.setId(String.valueOf(SecurityUtils.getUserId()));
        }
        return R.ok(userConcernService.pageUserConcern(baseQuery));
    }

    @PostMapping("/page/hot")
    @Operation(summary = "查询热门主播")
    public R<Page<AppUserConcernVO>> hot(@RequestBody AppBaseDTO baseQuery) {
        return R.ok(new Page<>());
    }

    @PostMapping("/page/fans")
    @Operation(summary = "查询用户关注的主播列表")
    public R<Page<AppUserConcernVO>> pageUserFans(@RequestBody BaseQuery baseQuery) {
        if (StringUtils.isBlank(baseQuery.getId())) {
            baseQuery.setId(String.valueOf(SecurityUtils.getUserId()));
        }
        return R.ok(userConcernService.pageUserFans(baseQuery));
    }

    @PostMapping("/page/getFans")
    @Operation(summary = "查询用户的粉丝列表")
    public R<IPage<AppUserConcernVO>> getFans(@RequestBody BaseQuery dto) {
        if(StringUtils.isBlank(dto.getId())){
            dto.setId(String.valueOf(SecurityUtils.getUserId()));
        }
        return R.ok(userConcernService.getFansPage(dto));
    }

    @PostMapping("/ignoreAttention/{userId}")
    @Operation(summary = "忽略关注")
    public R<?> ignoreAttention(@PathVariable Long userId){
       Wrapper<AppUserConcern> updateWrapper = Wrappers.<AppUserConcern>lambdaUpdate()
                .eq(AppUserConcern::getUserId, userId)
                .eq(AppUserConcern::getConcernId, SecurityUtils.getUserId());

        AppUserConcern appUserConcern = new AppUserConcern();
        appUserConcern.setExhibition(0);
        userConcernService.update(appUserConcern, updateWrapper);
        return R.ok();
    }

    @PostMapping("/page/friend")
    @Operation(summary = "查询用户好友列表")
    public R<Page<AppUserConcernVO>> pageUserFriend(@RequestBody BaseQuery baseQuery) {
        if (StringUtils.isBlank(baseQuery.getId())) {
            baseQuery.setId(String.valueOf(SecurityUtils.getUserId()));
        }
        return R.ok(userConcernService.pageUserFriend(baseQuery));
    }

    @GetMapping("/checkUserConcern/{userId}/{concernId}")
    @Operation(summary = "判断是否关注", description = "判断userId是否关注concernId, 返回1表示已关注，0未关注")
    public R<Integer> checkUserConcern(@PathVariable Long userId, @PathVariable Long concernId){
        Integer integer = userConcernService.checkUserConcern(userId, concernId);
        return R.ok(integer);
    }


    @PostMapping("/page/getInteractionUser/{type}")
    @Operation(summary = "查询点赞/评论用户列表", description ="type传值： dz点赞，pl评论，all所有用户" )
    public R<IPage<AppUserConcernVO>> getInteractionUser(@RequestBody BaseQuery dto, @PathVariable String type) {
        return R.ok(userConcernService.getInteractionUser(dto, type));
    }


    @PostMapping("/page/followUser")
    @Operation(summary = "查询用户关注的用户列表")
    public R<Page<AppUserConcernVO>> followUser(@RequestBody BaseQuery baseQuery) {
        if (StringUtils.isBlank(baseQuery.getId())) {
            baseQuery.setId(String.valueOf(SecurityUtils.getUserId()));
        }
        return R.ok(userConcernService.followUser(baseQuery));
    }
}
